package com.ruoyi.fly.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.fly.domain.AgentStoreCar;
import com.ruoyi.fly.domain.AgentUser;
import com.ruoyi.fly.domain.Store;
import com.ruoyi.fly.domain.vo.AgentUserSearchVo;
import com.ruoyi.fly.domain.vo.AgentUserVo;
import com.ruoyi.fly.domain.vo.DeleteVo;
import com.ruoyi.fly.service.AgentStoreCarService;
import com.ruoyi.fly.service.AgentUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户信息Controller
 */
@RestController
@RequestMapping("/agentUser")
@Api(tags = "代理账号")
public class AgentUserController extends BaseController {

    @Autowired
    private AgentUserService agentUserService;

    @Autowired
    private AgentStoreCarService agentStoreCarService;
    /**
     * 查询用户信息列表
     */
    @ApiOperation(value = "查询代理账号列表")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody AgentUserSearchVo sysUser) {
        PageDomain pageDomain = new PageDomain();
        BeanUtils.copyProperties(sysUser,pageDomain);
        startPage(pageDomain);
        List<AgentUser> list = agentUserService.selectSysUserList(sysUser);
        return getDataTable(list);
    }


    /**
     * 获取用户信息详细信息
     */
    @ApiOperation(value = "根据id获取代理账号详细信息")
    @GetMapping(value = "/select/{userId}")
    public R<AgentUser> getInfo(@PathVariable("userId") String userId) {
        if (userId == null){
            return R.fail("参数不能为空");
        }
        if (agentUserService.selectSysUserByUserId(userId) == null){
            return R.fail("用户不存在");
        }
        return R.ok(agentUserService.selectSysUserByUserId(userId));
    }

    /**
     * 新增用户信息
     */
    @ApiOperation(value = "新增代理账号")
    @PostMapping("/add")
    public R add(@RequestBody AgentUserVo sysUser) {
        if (StringUtils.isNotBlank(sysUser.getUserName()) || StringUtils.isNotBlank(sysUser.getNickName())
                || StringUtils.isNotBlank(sysUser.getPassword())){
            return R.fail("用户名,昵称,密码,账号类型不能为空");
        }
        return agentUserService.insertSysUser(sysUser);
    }

    /**
     * 修改用户信息
     */
    @ApiOperation(value = "修改代理账号")
    @PostMapping("/edit")
    public R edit(@RequestBody AgentUserVo sysUser) {
        return agentUserService.updateSysUser(sysUser);
    }

    /**
     * 删除用户信息
     */
    @ApiOperation(value = "删除代理账号")
    @PostMapping("/delete")
    public R remove(@RequestBody DeleteVo deleteVo) {
        if (StringUtils.isEmpty(deleteVo.getIds())){
            return R.fail("参数不能为空");
        }
        return agentUserService.deleteSysUserByUserIds(deleteVo.getIds());
    }


    @ApiOperation(value = "新增商铺车辆绑定")
    @PostMapping("/addAgentStoreCar")
    public R addAgentStoreCar(@RequestBody AgentStoreCar agentStoreCar) {
        return agentStoreCarService.insertAgentStoreCar(agentStoreCar);
    }
    @ApiOperation(value = "删除商铺车辆绑定")
    @PostMapping("/deleteAgentStoreCar")
    public R deleteAgentStoreCar(@RequestBody AgentStoreCar agentStoreCar) {
        return agentStoreCarService.deleteAgentStoreCar(agentStoreCar)>0 ? R.ok("成功") : R.fail("失败");
    }
}
